package com.wk.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wk.common.bean.Resp;
import com.wk.wms.entity.WareSku;
import com.wk.wms.service.WareSkuService;
import com.wk.wms.vo.SkuLockVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/waresku")
public class WareSkuController {

    @Autowired
    private WareSkuService  wareSkuService;

    /**
     * 检查并锁定下单商品的库存
     */
    @PostMapping
    public Resp<String> checkAndLockStore(@RequestBody List<SkuLockVO> skuLockVOs){
        String msg = wareSkuService.checkAndLockStore(skuLockVOs);
        if (StringUtils.isBlank(msg)) {
            return Resp.ok(null);
        }
        return Resp.fail(msg);
    }

    /**
     * 查询分页数据
     */
    @GetMapping
    public IPage<WareSku> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "20") int pageSize){
        return wareSkuService.page(new Page<>(pageNum,pageSize));
    }

    /**
     * 根据skuId查询对应商品库存
     */
    @GetMapping("{skuId}")
    public Resp<List<WareSku>> queryBySkuId(@PathVariable Long skuId){
        return Resp.ok(wareSkuService.list(new QueryWrapper<WareSku>().eq(skuId!=null,"sku_id",skuId)));
    }

    /**
     * 根据id查询
     */
    @GetMapping("info/{id}")
    public WareSku getById(@PathVariable("id") Long id) {
        return wareSkuService.getById(id);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Resp<Object> save(@RequestBody WareSku wareSku){
        wareSkuService.save(wareSku);

        return Resp.ok(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Resp<Object> update(@RequestBody WareSku wareSku){
        wareSkuService.updateById(wareSku);

        return Resp.ok(null);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Resp<Object> delete(@RequestBody Long[] ids){
        wareSkuService.removeByIds(Arrays.asList(ids));

        return Resp.ok(null);
    }

}

